//
//  CYTicketDetailHeaderView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/18.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYTicketDetailHeaderView.h"
@interface CYTicketDetailHeaderView()
@property (nonatomic, strong) CYLabel *nameLBL;//彩种
@property (nonatomic, strong) CYLabel *dateLBL;//日期
@property (nonatomic, strong) CYLabel *timeLBL;//时间
@property (nonatomic, strong) CYImageView *dateIMG;//日期图片
@property (nonatomic, strong) CYLabel *numLBL;//每天中奖次数
@end
@implementation CYTicketDetailHeaderView
- (instancetype)initWithColor:(UIColor *)color{
    self = [super initWithColor:color];
    if (self) {
        
    }

    return self;
}

- (CYLabel *)nameLBL{
    if (!_nameLBL) {
       _nameLBL = [[CYLabel alloc]initWithText:_name size:AL(PT(32)) weight:UIFontWeightBold];
        _nameLBL.textColor = ColorHex(0x333333);
        [self addSubview:_nameLBL];
        
        [_nameLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(AL(PT(20)));
            make.top.equalTo(self).with.offset(AL(PT(20)));
            make.size.mas_equalTo(_nameLBL.bounds.size);
        }];
    }
    return _nameLBL;
}

- (CYImageView *)dateIMG{
    if (!_dateIMG) {
       _dateIMG = [[CYImageView alloc]initWithImageName:@"time" size:CGSizeMake(AL(10), AL(10)) isRadius:NO];
        [self addSubview:_dateIMG];
        
        [_dateIMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_nameLBL.mas_right).with.offset(AL(PT(10)));
            make.centerY.equalTo(_nameLBL);
            make.size.mas_equalTo(_dateIMG.bounds.size);
        }];
    }
    return _dateIMG;
}

- (CYLabel *)dateLBL{
    if (!_dateLBL) {
       _dateLBL = [[CYLabel alloc]initWithText:[self dateTitle] size:AL(PT(22)) textcolor:ColorHex(0x333333)];
        [self addSubview:_dateLBL];
        
        [_dateLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_dateIMG.mas_right).with.offset(0);
            make.centerY.equalTo(_nameLBL);
            make.size.mas_equalTo(_dateLBL.bounds.size);
        }];
    }
    return _dateLBL;
}

- (CYLabel *)timeLBL{
    if (!_timeLBL) {
        _timeLBL = [[CYLabel alloc]initWithText:_time size:AL(PT(22)) textcolor:ColorHex(0x333333)];
        [self addSubview:_timeLBL];
        
        [_timeLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_dateLBL.mas_right).with.offset(AL(PT(10)));
            make.centerY.equalTo(_nameLBL);
            make.size.mas_equalTo(_timeLBL.bounds.size);
        }];
    }
    return _timeLBL;
}

- (CYLabel *)numLBL{
    if (!_numLBL) {
       _numLBL = [[CYLabel alloc]initWithText:[NSString stringWithFormat:@"每天%@次中奖机会",_number] size:AL(PT(22)) textcolor:ColorHex(0x888888)];
        [self addSubview:_numLBL];
        
        [_numLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self).with.offset(- AL(PT(20)));
            make.size.mas_equalTo(_numLBL.bounds.size);
        }];
    }
    return _numLBL;
}

//彩种
- (void)setName:(NSString *)name{
    if (_name != name)
        _name = name;
    self.nameLBL.text = _name;
    
}

//期号
- (void)setAction_num:(NSString *)action_num{
    if (_action_num != action_num)
        _action_num = action_num;
    self.dateIMG.hidden = NO;
    self.dateLBL.text = [self dateTitle];

}

//时间
- (void)setTime:(NSString *)time{
    if (_time != time)
        _time = time;
    self.timeLBL.text = time;

}

//每天中奖次数
- (void)setNumber:(NSString *)number{
    if (_number != number)
        _number = number;
    self.numLBL.text = [NSString stringWithFormat:@"每天%@次中奖机会",number];
}

//开奖号码
- (void)setResults:(NSArray *)results{
    if (_results != results)
        _results = results;
    [self setupSubView];

}

- (NSString *)dateTitle{
    NSString *string = [NSString stringWithFormat:@"%@",_action_num];
    if (string.length < 8)
        return [NSString stringWithFormat:@"第-期"];
    NSString *date = [string substringToIndex:8];
    NSString *qihao = [string substringFromIndex:8];
    return [NSString stringWithFormat:@"第%@-%@期", date, qihao];
}

- (void)setupSubView{
    CGFloat radius = (_results.count < 10)?AL(PT(80)):AL(PT(50));//开奖号码半径
    CGFloat minus_width = (SCREEN_WIDTH - _results.count * radius) / (_results.count + 1);//开奖号码间隙宽度
    CGFloat font_size = (_results.count < 10)?AL(PT(42)):AL(PT(32));
    for (int i = 0; i < _results.count; i ++) {
        
      CYLabel *label = [[CYLabel alloc]initWithText:_results[i] size:font_size weight:UIFontWeightBold];
        label.textAlignment = NSTextAlignmentCenter;
        label.bounds = CGRectMake(0.0, 0.0, radius, radius);
        label.textColor = [UIColor whiteColor];
        label.layer.cornerRadius = radius / 2.0;
        label.layer.masksToBounds = YES;
        label.backgroundColor = THEME_COLOR;
        [self addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(minus_width + (radius + minus_width) * i);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(label.bounds.size);
        }];
    }

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
